#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#define NUM 1024

struct MyFILE_{
    int fd;
    char buffer[1024];
    int end;//缓冲区的结尾:
};

typedef struct MyFILE_ MyFILE;

MyFILE *fopen_(const char *pathname,const char *mode)
{
    assert(pathname);
    assert(mode);
    
    MyFILE *fp = NULL;
    if(strcmp(mode,"r")== 0)
    {

    }
    else if(strcmp(mode,"r+") == 0)
    {

    }
    else if(strcmp(mode,"w") == 0)
    {
        int fd = open(pathname,O_WRONLY | O_CREAT | O_TRUNC,0666);
        if(fd >= 0)
        {     
            fp = (MyFILE*) malloc(sizeof (MyFILE));
            memset(fp,0,sizeof (MyFILE));
            fp->fd = fd;
        }
    }
    else if(strcmp(mode,"w+") == 0)
    {

    }
    else if(strcmp(mode,"a") == 0)
    {

    }
    else if(strcmp(mode,"a+") == 0)
    {

    }else{
        //什么都不做
    }
    return fp;
}

void fputs_(const char *massage,MyFILE *fp)
{
    assert(massage);
    assert(fp);
    
    strcpy(fp->buffer+fp->end,massage);//abcde\0
    fp->end += strlen(massage);

    printf("%s\n",fp->buffer);
    if(fp->fd == 0)
    {

    }else if(fp->fd ==1)
    {
        if(fp->buffer[fp->end-1] == '\n')
        {
            fprintf(stderr,"fflush: %s",fp->buffer);
            write(fp->fd,fp->buffer,fp->end);
            fp->end = 0;
        }
    }else if(fp->fd == 2)
    {

    }else{
        //其他文件
    }
}


void fflush_(MyFILE *fp)
{
    assert(fp);
    if(fp->end != 0)
    {
        write(fp->fd,fp->buffer,fp->end);
        syncfs(fp->fd);//将数据写入磁盘
        fp->end = 0;
    }
}

void fclose_(MyFILE *fp)
{
    assert(fp);
    fflush_(fp);
    close(fp->fd);
    free(fp);
}
int main()
{
    close(1);
    MyFILE *fp = fopen_("./log.txt","w");
    if(fp == NULL)
    {
        printf("open file error");
        return 1;
    }
    
    fputs_("one: hello world",fp);
    sleep(1);
    fputs_(" two: hello world",fp);
    sleep(1);
    fputs_(" three: hello world\n",fp);
    sleep(1);
    fputs_(" four: hello world\n",fp);
    sleep(1);

    fclose_(fp);
    return 0;
}



//int main(int argc,char *argv[])
//{
//    if(argc!=2)
//    {
//         return 2;
//    }
//    int fd = open("log.txt",O_WRONLY | O_CREAT | O_TRUNC,0666);
//    //int fd = open("log.txt",O_WRONLY | O_CREAT | O_APPEND);
//    if(fd<0)
//    {
//        perror("open");
//    }
//    dup2(fd,1);
//    fprintf(stdout,"%s\n",argv[1]);
//
//    close(fd);
//
//
//
//    //close(1);
//    //int fd = open("log.txt",O_WRONLY | O_TRUNC | O_CREAT);
//    //int fd = open("log.txt",O_WRONLY | O_APPEND | O_CREAT);
//    //if(fd<0)
//    //{
//    //    perror("open");
//    //    return 1;
//    //}
//    //fprintf(stdout,"you can see me ,success \n");
//
//    //printf("fd: %d\n",fd);
//
//    //char buffer[64];
//    //fgets(buffer,sizeof buffer,stdin);
//    //printf("%s\n",buffer);
//
//
//
//    //close(0);
//
//    //int fd = open("log.txt",O_WRONLY | O_CREAT | O_TRUNC,0666);
//    //if(fd<0)
//    //{
//    //    perror("open");
//    //}
//
//    //printf("%d\n",fd);
//
//    //close(fd);
//    return 0;
//}
